package com.itheima.test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();


        Scanner sc = new Scanner(System.in);
       lo: while (true){
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");




            String choice = sc.next();

            switch (choice){
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    update(list);
                    break;
                case "4":
//                    System.out.println("查看学生");
                    showStudent(list);
                    break;
                case "5":
                    System.out.println("退出学生");
                    break lo;
                default:
                    System.out.println("你输入的信息有错误");
                    break;
            }

        }
    }

    public static void update(ArrayList<Student> list) {
        System.out.println("请输入你要修改的学号");
        Scanner sc =new Scanner(System.in);
        String sid = sc.next();
        int id= getIndex(list, sid);

        if(id==-1){
            System.out.println("你要修改的信息不存在");
        }else {
            System.out.println("请输入你要修改的学生姓名");
            String name = sc.next();
            System.out.println("请输入你要修改的学生年龄");
             int age= sc.nextInt();
            System.out.println("请输入你要修改的学生生日");
             String b= sc.next();
            Student stu = new Student(sid,name,age,b);

            list.set(id,stu);
        }
    }

    public static void deleteStudent(ArrayList<Student> list) {
       System.out.println("请输入你要删除的学号");
       Scanner sc =new Scanner(System.in);
       String sid = sc.next();
       int id= getIndex(list, sid);

       if(id==-1){
           System.out.println("你要删除的信息不存在");
       }else {
           list.remove(id);
           System.out.println("删除成功");
       }

   }

    public  static void showStudent(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("还没有信息，需要手动添加");
            return;
        }
      System.out.println("学号\t姓名\t年龄\t生日");
      for (int i = 0; i < list.size(); i++) {
          Student stu = list.get(i);
          System.out.println(stu.getSid()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getBirthday());
      }
    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner sc =new Scanner(System.in);
        String sid ;
        while(true){
            System.out.println("请输入学号");
            sid = sc.next();
            int index = getIndex(list, sid);
            if(index==-1){
                break;
            }
        }

        System.out.println("请输入学生姓名");
        String sname = sc.next();
        System.out.println("请输入学生年龄");
        int sAge = sc.nextInt();
        System.out.println("请输入学生生日");
        String sb = sc.next();

        Student stu = new Student(sid,sname,sAge,sb);
        list.add(stu);

        System.out.println("添加成功");

    }

    public static int getIndex(ArrayList<Student> list,String sid){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            if(id.equals(sid)){
                index=i;
            }
        }
        return index;
    }


}
